package com.yanchangapp.adapter;

import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.zxl.baselib.util.ui.ResHelper;
import com.yanchangapp.R;
import com.yanchangapp.commom.AppConstant;
import com.yanchangapp.model.bean.CntUserBean;

import java.util.List;

/**
 * Created by apple on 2019/7/16.
 * description:联系人部门列表adapter
 */
public class CntDpAdapter extends BaseQuickAdapter<CntUserBean, BaseViewHolder> {
    private boolean hasBound = false;
    private CntSelectListener mCntSelectListener;
    public CntDpAdapter(int layoutResId, @Nullable List<CntUserBean> data,CntSelectListener cntSelectListener) {
        super(layoutResId, data);
        this.mCntSelectListener = cntSelectListener;
    }

    @Override
    protected void convert(BaseViewHolder helper, CntUserBean item) {
        TextView tvIndex = helper.getView(R.id.tvIndex);
        String text = "";
        Integer structStep = item.getStructStep();

        if (structStep != null && structStep == 1){
            int position = helper.getAdapterPosition();
            Integer currType = item.getIsFriendCompany();
            if (position == 0){
                if (currType != null &&  currType == 2){
                    text = "本单位";
                }else {
                    text ="外单位";
                }
            }else {
                Integer beforeType = mData.get(position - 1).getIsFriendCompany();
                if (beforeType != null && (beforeType.intValue() != currType.intValue())){
                    if (currType == 2){
                        text = "本单位";
                    }else {
                        text ="外单位";
                    }
                }
            }
        }
        if (TextUtils.isEmpty(text)){
            tvIndex.setVisibility(View.GONE);
        }else {
            tvIndex.setText(text);
            tvIndex.setVisibility(View.VISIBLE);
        }

        LinearLayout mLlDepartment =  helper.getView(R.id.llDepartment);
        LinearLayout mLlUser = helper.getView(R.id.llUser);
        CheckBox checkBox =  helper.getView(R.id.cbDp);
        if (item.getFlag() != null && (item.getFlag() == AppConstant.FLAG_IS_USER)){
            mLlDepartment.setVisibility(View.GONE);
            mLlUser.setVisibility(View.VISIBLE);
            helper.setText(R.id.tvMailSickName,item.getName());
            helper.setText(R.id.tvMailEmail,item.getAddress());
            // 用户
            if (helper.getAdapterPosition() == mData.size()-1){
                mLlUser.setBackground(null);
            }else {
                mLlUser.setBackground(ResHelper.getDrawable(R.drawable.shape_bottom_line));
            }
        }else {
            mLlDepartment.setVisibility(View.VISIBLE);
            mLlUser.setVisibility(View.GONE);
            helper.addOnClickListener(R.id.tvDpNext);
            helper.setText(R.id.tvDpName,item.getName());
            helper.setText(R.id.tvSum,mContext.getString(R.string.person_inner_choose,String.valueOf(item.getSelectedSize()),String.valueOf(item.getSum())));
            if (item.getChildren().size() == 0){
                helper.setVisible(R.id.tvDpNext,false);
            }else {
                helper.setVisible(R.id.tvDpNext,true);
            }
            // 部门
            if (helper.getAdapterPosition() == mData.size()-1){
                mLlDepartment.setBackground(null);
            }else {
                mLlDepartment.setBackground(ResHelper.getDrawable(R.drawable.shape_bottom_line));
            }
        }
        checkBox.setChecked(item.isChecked());
        checkBox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mCntSelectListener != null){
                    mCntSelectListener.onCheckChangedListener(helper.getAdapterPosition(),checkBox.isChecked(),item);
                }
            }
        });

    }

    public interface CntSelectListener{
        void onCheckChangedListener(int position,boolean state,CntUserBean cntUserBean);
    }
}
